<template>
  <el-row :gutter="24" :style="{ paddingRight: collapseTree ? 'var(--mian-left-width)' : '0' }">
    <!-- 左侧部门树 -->
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-[calc(100vh-var(--heard-height))]" :isTree="true" v-if="!collapseTree">
        <DeptTree @node-click="handleDeptNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="collapseTree ? 24 : 20" :xs="24">
      <ContentWrap ref="form">
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="用户昵称" prop="nickname">
            <el-input
              v-model="queryParams.nickname"
              placeholder="请输入用户昵称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="用户账号" prop="username">
            <el-input
              v-model="queryParams.username"
              placeholder="请输入用户账号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="用户编码" prop="userCode">
            <el-input
              v-model="queryParams.userCode"
              placeholder="请输入用户编码"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="用户类型" prop="userType">
            <el-select
              v-model="queryParams.userType"
              placeholder="请选择用户类型"
              clearable
              class="!w-240px"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.MEMBER_USER_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select
              v-model="queryParams.sex"
              placeholder="请选择性别"
              clearable
              class="!w-240px"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="身份证号" prop="idNumber">
            <el-input
              v-model="queryParams.idNumber"
              placeholder="请输入身份证号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="审核状态" prop="reviewStatus">
            <el-select
              v-model="queryParams.reviewStatus"
              placeholder="请选择审核状态"
              clearable
              class="!w-240px"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.MEMBER_REVIEW_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"
              ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
            >
            <el-button @click="resetQuery"
              ><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button
            >
            <el-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['member:user-review-record:export']"
            >
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap :setPadding="false">
        <el-table v-loading="loading" :data="list" stripe :max-height="tableHeight">
          <el-table-column label="用户昵称" prop="nickname" show-overflow-tooltip fixed="left" />
          <el-table-column label="用户账号" prop="username" show-overflow-tooltip />
          <el-table-column label="手机号" prop="mobile" show-overflow-tooltip />
          <el-table-column label="用户编码" prop="userCode" show-overflow-tooltip />
          <el-table-column label="用户类型 " prop="userType">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.MEMBER_USER_TYPE" :value="scope.row.userType" />
            </template>
          </el-table-column>
          <el-table-column label="性别" prop="sex">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.sex" />
            </template>
          </el-table-column>
          <el-table-column label="身份证号" prop="idNumber" show-overflow-tooltip />
          <el-table-column label="家庭住址" prop="address" show-overflow-tooltip />
          <el-table-column label="审核状态" prop="reviewStatus">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.MEMBER_REVIEW_STATUS" :value="scope.row.reviewStatus" />
            </template>
          </el-table-column>
          <el-table-column label="审核备注" prop="reviewRemark" show-overflow-tooltip />
          <el-table-column
            label="创建时间"
            prop="createTime"
            :formatter="dateFormatter"
            show-overflow-tooltip
            width="180"
          />
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>

  <!-- 表单弹窗：添加/修改 -->
  <UserReviewRecordForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="UserReviewRecord">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as UserReviewRecordApi from '@/api/member/userReviewRecord'
import DeptTree from '@/components/DeptTree.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()
const collapseTree = computed(() => appStore.getCollapseTree)

const message = useMessage() // 消息弹窗
//const { t } = useI18n() // 国际化
const form = ref(null)
const { width, height } = useElementSize(form)
const tableHeight = useWindowSize(78 + 76, height)

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickname: null,
  avatar: null,
  status: null,
  mobile: null,
  password: null,
  username: null,
  userCode: null,
  userType: null,
  sex: null,
  schoolId: null,
  collegeId: null,
  majorId: null,
  classesId: null,
  longitude: null,
  latitude: null,
  idNumber: null,
  email: null,
  address: null,
  remarks: null,
  userDevid: null,
  registerIp: null,
  loginIp: null,
  loginDate: [],
  systemUsersId: null,
  systemUsersBind: null,
  bindingId: null,
  createTime: [],
  deptId: null,
  reviewStatus: null,
  reviewRemark: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 处理部门被点击 */
const handleDeptNodeClick = async (row) => {
  queryParams.deptId = row.id
  await getList()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserReviewRecordApi.getUserReviewRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 删除按钮操作 */
// const handleDelete = async (id: number) => {
//   try {
//     // 删除的二次确认
//     await message.delConfirm()
//     // 发起删除
//     await UserReviewRecordApi.deleteUserReviewRecord(id)
//     message.success(t('common.delSuccess'))
//     // 刷新列表
//     await getList()
//   } catch {}
// }

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserReviewRecordApi.exportUserReviewRecord(queryParams)
    download.excel(data, '会员用户审核记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})

/* 切换页面展开当前页面树 */
onUnmounted(() => {
  appStore.setCollapseTree(false)
})
</script>
